package com.xinjian.jianba.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xinjian.jianba.dto.MomLikeDto;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.ValidatorService;
import com.xinjian.jianba.service.impl.MomLikeServiceImpl;

@RestController
@RequestMapping(value="/api/v3/jianba/MomLike")
public class MomLikeControllor extends AbstractController {
	@Autowired
	private MomLikeServiceImpl momLikeServiceImpl;
	@Autowired
	private ValidatorService validatorService;
	@PostMapping("/like")
	public String Momlike(@RequestBody MomLikeDto momLikeDto){
		try {
			validatorService.valid(momLikeDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		String action=momLikeDto.getAction();
		if("Like".equals(action)){
			Long likeId=momLikeServiceImpl.insertMomLikebyMonId(momLikeDto);
			if(likeId==-1l){
				return this.responseClientJsonData(ResponseCode.ERROR,"已点赞");
			}
			return this.responseClientJsonData(ResponseCode.SUCCESS, "点赞成功");
		}else if("CancleLike".equals(action)){
			Long re=momLikeServiceImpl.cancleLike(momLikeDto);
			if(re==0){
				return this.responseClientJsonData(ResponseCode.ERROR, "已取消点赞");
			}
			return this.responseClientJsonData(ResponseCode.SUCCESS, "取消点赞成功");
		}else{
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "action不合法");
		}
	}
}
